﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Itop.YHPC.demo {
    public partial class demo2ajax : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            string action = Request.QueryString["action"];
            //读取上传的数据流
            System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream, Encoding.UTF8);
            string json = sr.ReadToEnd();
            json = util.U2G(json);//字符串中的中文unicode字符转成中文

            string ret = "";//返回字符串
            if (action == "read") {//查询数据
                ret = read(json);
            } else if (action == "create") {//增加对象
                ret = create(json);
            } else if (action == "update") {//更新对象
                ret = update(json);
            } else if (action == "delete") {//删除对象
                ret = delete(json);
            }
            Response.Write(ret);
            Response.End();
        }
        private string read(string json) {
            //json里可能有分页信息
            //string str = "[{firstName : 'Lou中is', lastName  : 'Dobbs',dob       : '12/21/34',  userName  : 'ldobbs' }]";
            demo2Model model = new demo2Model() { first = "Lou中is", last = "Dobbs", email = "a@a.com", id = 1 };
            string ret = Newtonsoft.Json.JsonConvert.SerializeObject(model);
            List<demo2Model> list = new List<demo2Model>() { model };
            retModelList retm = new retModelList() { message = "read recoder", data = list };
            return Newtonsoft.Json.JsonConvert.SerializeObject(retm);
        }
        private string update(string json) {
            if (string.IsNullOrEmpty(json)) return "";
            IList<demo2Model> list = null;//
            try {
                list = new List<demo2Model>();//
                list.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<retModel>(json).data);
            } catch {//可能是对象数组
                list = Newtonsoft.Json.JsonConvert.DeserializeObject<retModelList>(json).data;
            }
            if (list==null) {
                
            } 
            foreach (demo2Model model in list) {
                //create code;
                //model.id = 100;
            }

            retModelList retm = new retModelList() { message = "Update recoder", data=list };

            return Newtonsoft.Json.JsonConvert.SerializeObject(retm); 
            //return "{success:true}";
        }
        private string create(string json) {
            if (string.IsNullOrEmpty(json)) return "";
            IList<demo2Model> list = null;//
            try {
                list = new List<demo2Model>();//
                list.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<retModel>(json).data);
            } catch {//可能是对象数组
                list = Newtonsoft.Json.JsonConvert.DeserializeObject<retModelList>(json).data;
            }
            if (list == null) {
                                
            }
            foreach(demo2Model model in list) {
                model.id =new Random().Next(10,99);
            }
            retModelList retm = new retModelList() { data = list };

            return Newtonsoft.Json.JsonConvert.SerializeObject(retm); 
        }
        private string delete(string json) {
            if (string.IsNullOrEmpty(json)) return "";
            IList<demo2Model> list = null;//
            try {
                list = new List<demo2Model>();//
                list.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<retModel>(json).data);
            } catch {//可能是对象数组
                list = Newtonsoft.Json.JsonConvert.DeserializeObject<retModelList>(json).data;
            }
            if (list == null) {

            }
            foreach (demo2Model model in list) {
                //create code;
                //model.id = 100;
            }
            return "{success:true}";
        }
        class retModelList {
            public  bool success = true;
            public IList<demo2Model> data = null;
            public string message = "Created record";
        }
        class retModel{
            public bool success = true;
            public demo2Model data = null;
            public string message = "Created record";
        }
    }
}
